package com.wyh.shiro2_ssm.shiro;

import com.wyh.shiro2_ssm.pojo.SysPermission;
import com.wyh.shiro2_ssm.pojo.SysUser;
import com.wyh.shiro2_ssm.service.SysPermissionService;
import com.wyh.shiro2_ssm.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
* 注入
* */
public class MyShiroRealm extends AuthorizingRealm {
    Logger log = LoggerFactory.getLogger(MyShiroRealm.class);
    //    注入业务对象
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysPermissionService permissionService;
    //    授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String principal = (String )principalCollection.getPrimaryPrincipal();
//        根据用户名查询用户拥有的权限清单
        List<SysPermission> sysPermissions = permissionService.findPermsByLoginName(principal);
        if(sysPermissions!=null) {
//            权限去重
            Set<String> perms = new HashSet<>();
            for (SysPermission perm : sysPermissions) {
                perms.add(perm.getPerName());
                log.info(perm.getPerName());
            }

            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//            授权
            authorizationInfo.setStringPermissions(perms);
            return authorizationInfo;
        }
        return null;
    }

    //    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//       获取用户信息
        String  username = (String) token.getPrincipal();
        SysUser sysUser = userService.findUserByName(username);
        System.out.println(sysUser);
        if(sysUser!=null){
//            当前realm对象的name
            String realmName = getName();
//            System.out.println("username:"+username);
//            使用用户名做盐值
            ByteSource salt = ByteSource.Util.bytes(username);
//            salt{YWRtaW4=}
            System.out.println("salt{"+salt+"}");
            String pwd = sysUser.getPassword();
//            封装用户信息，构建AuthenticationInfo对象返回
            /*
            带盐值的构造器
            * public SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName) {
        this.principals = new SimplePrincipalCollection(principal, realmName);
        this.credentials = hashedCredentials;
        this.credentialsSalt = credentialsSalt;
    }
            * */
            SimpleAuthenticationInfo authInfo = new SimpleAuthenticationInfo(username,pwd,salt,realmName);
//            authInfo{admin}
//            System.out.println("authInfo{"+authInfo.toString()+"}");
//            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, pwd, this.getName());
            return authInfo;
        }
        return null;
    }
}
